﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YwhCore
{
    public class UnitService
    {
        public static QueryRsp<Unit> Save(Unit unit)
        {
            QueryRsp<Unit> rsp = new QueryRsp<Unit>();

            ConfigService.Fsql.InsertOrUpdate<Unit>().SetSource(unit).ExecuteAffrows();

            return rsp;
        }

        public static QueryRsp<Unit> List(int PageNumber, int PageSize, int phaseNo = 0, int buildingNo = 0)
        {
            long l = 0;
            QueryRsp<Unit> query = new QueryRsp<Unit>();
            if (phaseNo == 0)
            {
                query.Items = ConfigService.Fsql.Select<Unit>().Count(out l).Page(PageNumber, PageSize).ToList();
            }
            else
            {
                query.Items = ConfigService.Fsql.Select<Unit>().Where(f => f.PhaseNo == phaseNo).Count(out l).Page(PageNumber, PageSize).ToList();
            }
            //query.Items = ConfigService.Fsql.Select<Unit>().Count(out l).Page(PageNumber, PageSize).ToList();
            query.Total = (int)l;
            return query;
        }

        public static QueryRsp<Unit> Update(Unit unit)
        {
            QueryRsp<Unit> rsp = new QueryRsp<Unit>();

            ConfigService.Fsql.Update<Unit>().Set(a => a.PhaseNo, 1).ExecuteAffrows();

            return rsp;
        }

        public static QueryRsp<Unit> Delete(Unit unit)
        {
            QueryRsp<Unit> rsp = new QueryRsp<Unit>();

            ConfigService.Fsql.Delete<Unit>().Where(x => x.UnitNo == unit.UnitNo).ExecuteAffrows();

            return rsp;
        }
    }
}
